import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue2 from "@vitejs/plugin-vue2";
import { loadEnv } from "vite";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
    const env = loadEnv(mode, process.cwd(), "");
    return {
        plugins: [vue2()],
        resolve: {
            alias: {
                "@": fileURLToPath(new URL("./src", import.meta.url)),
            },
            extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
        },
        css: {
            preprocessorOptions: {
                less: {
                    javascriptEnabled: true,
                    math: "always",
                },
            },
        },
        server: {
            host: "0.0.0.0",
            port: 5173,
            open: true,
            proxy: {
                "/adminapi": {
                    target: env.VITE_API_BASE_URL, // 目标服务器地址
                    changeOrigin: true, // 是否改变源地址
                    rewrite: (path) => path.replace(/^\/adminapi/, ""), // 重写路径
                },
            },
        },
    };
});
